<?PHP
class RussianPage extends webPage
{
	function RussianPage()
	{
		parent::webPage();
	}
	
	public function OnInit($sender, $args)
	{
		$this->initialize();
		
		if ( array_key_exists('async', $_REQUEST) )
		{
			switch ( $_REQUEST['async'] )
			{
				case 'insertWord':
					$this->insertWord($_REQUEST['language'], $_REQUEST['word'], $_REQUEST['description']);
				break;
				case 'deleteWord':
					$this->deleteWord($_REQUEST['language'], $_REQUEST['id']);
				break;
				case 'getWord':
					$this->getWord($_REQUEST['language'], $_REQUEST['id']);
				break;
				case 'saveWord':
					$this->saveWord($_REQUEST['language'], array(
						'id' => $_REQUEST['id'],
						'word' => $_REQUEST['word'],
						'description' => $_REQUEST['description']
					));
				break;
				default:
					$resError = new xmlResult('Not supported operation');
					$resError->exitResult();
				break;
			}
		}
	}
	
	public function insertWord($language, $word, $description)
	{
		switch ( $language )
		{
			case 'en':
				$db = dict::get();
				$en = new English();
				$en->word = $word;
				$en->description = $description;
				$db->English->insert($en);
				$result = new xmlResult("Word: [$word] is added", 'ok');
			break;
			case 'ru':
				$db = dict::get();
				$ru = new Russian();
				$ru->word = $word;
				$ru->description = $description;
				$db->Russian->insert($ru);
				$result = new xmlResult("Word: [$word] is added", 'ok');
			break;
			default:
				$result = new xmlResult('Not supported');
		}
		$result->exitResult();
	}
	
	public function deleteWord($language, $id)
	{
		switch ( $language )
		{
			case 'en':
				$db = dict::get();
				$db->English->delete($id);
				$result = new xmlResult("The word[$id] has deleted!!!");
			default:
				$result = new xmlResult("Delete [$id] failed!!!");
		}
		$result->exitResult();
	}
	
	public function getWord($language, $id)
	{
		switch ( $language )
		{
			case 'en':
				$db = dict::get();
				$item = $db->English->byId($id);
				$result = new xmlResult('Word has been found', 'OK');
				$details = "<id>{$item->id}</id>";
				$details .= "<word>{$item->word}</word>";
				$details .= "<description>{$item->description}</description>";
				$result->setDetails($details);
			break;
			default:
				$result = new xmlResult("get word: not implemented ($language, $id)");
		}
		$result->exitResult();
	}
	public function saveWord($language, $assoc)
	{
		switch ( $language )
		{
			case 'en':
				$db = dict::get();
				$item = $db->English->byId($assoc['id']);
				if ( !empty($item) )
				{
					if ( isset($assoc['word']) )
					{
						$item->word = $assoc['word'];
					}
					if ( isset($assoc['description']) )
					{
						$item->description = $assoc['description'];
					}
				}
				$res = $db->English->update($item);
				$result = new xmlResult('save word executed', 'OK');
				$result->setDetails(print_r($res, true));
			break;
			default:
				$result = new xmlResult("save word: not implemented ($language, {$assoc['id']})");
		}
		$result->exitResult();
	}
	
	private function initialize()
	{
		$this->setTitle("Russian");
		$db = dict::get();
		$dataList = $db->Russian->table();
	
		foreach ( $dataList as $item )
		{
			$tr = new tagTr();

			$data = "{id:'{$item->id}', word:'{$item->word}',description:'{$item->description}'}";
			
			$td = new tagTd();
			$tagText = new htmlText($item->word);
			$tagA = new tagA(array('href' => 'javascript:void(0);', 'onclick'=>"menu.showDialog('editWord', $data);"));
			$tagA->addControl($tagText);
			$td->addControl($tagA);
			$tr->addControl($td);
			
			$td = new tagTd();
			$tagText = new htmlText($item->description);
			$td->addControl($tagText);
			$tr->addControl($td);

			$this->tableRussian->addControl($tr);
		}
	}
}
?>
